<?php
require_once ('..'.DIRECTORY_SEPARATOR.'initialize.php');
include_once(Utils::getPath(array('shared', 'template.php')));
include_once(Utils::getPath(array('repositories', 'solutionRepository.php')));
include_once(Utils::getPath(array('repositories', 'challengeRepository.php')));
include_once(Utils::getPath(array('repositories', 'userRepository.php')));

$template = new Template('application');
$template->initialize();

if(!isset($_GET['id']))
{
    Utils::goToErrorPage(StatusCodes::HTTP_BAD_REQUEST);
}
//Get Entities from Repositories
$solutionRep = new SolutionRepository();
$solution = $solutionRep->getById($_GET['id']);
//Check if id is wrong
if($solution == null)
{
    Utils::goToErrorPage();
}
$challengeRep = new ChallengeRepository();
$challenge = $challengeRep->getById($solution->getIdChallenge());
//Check if id is wrong
if($challenge == null)
{
    Utils::goToErrorPage();
}

//Set variables for html
$title = $challenge->getTitle();
$description = $solution->getDescription();
$filePath = $solution->getFilePath();
$id = $solution->getId();
$commentType = 2;
$solution_is_valid = $solution->getValid();
$filterUrlParam = '&amp;id='.$id;

$userRep = new UserRepository();
$solution_author = $userRep->getById($solution->getIdAuthor());

if(!$solutionRep->userCanAccessSolution($_SESSION['authenticatedId'], $solution))
{
    Utils::goToErrorPage(StatusCodes::HTTP_FORBIDDEN);
}
?>
        <div id = "item-view">
            <div id = "item-header">
                <div id="item-title">
                    <?php echo $title; ?>
                </div>
                <div id = "item-challenge">
                    Go to <a href="<?php echo Utils::getUrl('challenge/view.php?id='.$solution->getIdChallenge()) ?>">challenge</a>
                </div>
                <div id = "item-info">
                    Solution by <a href="<?php echo Utils::getUrl('profile.php?id='.$solution_author->getId()) ?>"><?php echo $solution_author->getUserName(); ?></a>
                </div>
            </div>
            <div id = "item-description">
                    <?php echo $description; ?>
            </div>
            <?php if($filePath != null && $filePath != "") { ?><a href="<?php echo Utils::getUrl($filePath) ?>">Download solution</a><?php } ?>
        </div>
<?php
    if($challenge->getIdAuthor() == $_SESSION['authenticatedId'])
    {
?>
            <p><input type="radio" name="valid" value="Yes" <?php if(isset($solution_is_valid) && $solution_is_valid) echo 'checked="checked"' ?> onclick="return updateValid(true, <?php echo $id; ?>)"/>Valid
            <input type="radio" name="valid" value="No" <?php if(isset($solution_is_valid) && !$solution_is_valid) echo 'checked="checked"' ?> onclick="return updateValid(false, <?php echo $id; ?>)"/>Not valid</p>
            <script type="text/javascript">
                $(document).ready(function(){
                    updateSolutionRateVisibility(<?php echo $solution_is_valid;?>);
                });
                function updateValid(value, solutionid)
                {
                    var clicked_data = {
                        valid : value,//checkbox.checked,
                        solution : solutionid
                    };
                    $.post(
                        'rate.php',
                        clicked_data,
                        function(data)
                        {
                            updateSolutionRateVisibility(data.result);
                        },
                        'json'
                    );
                }
                function updateSolutionRateVisibility(value)
                {
                    if(parseInt(value))
                    {
                        $("#solution-rate").show();
                    }
                    else
                    {
                        $("#solution-rate").hide();
                    }
                }
            </script>

        <div id = "solution-rate">
            <?php include(Utils::getPath(array('solution', 'rate.php'))); ?>
        </div>
<?php   
    }
?>
    <div id="comment-list">
<?php
include(Utils::getPath(array('comment', 'list.php')));
?>
</div>
<input type="hidden" id="url-list" value="<?php echo htmlentities(Utils::getUrl('comment/list.php?id='.$solution->getId().'&commentType=2')) ?>"/>
<script type="text/javascript" src="<?php echo Utils::getUrl('scripts/comment.js') ?>"></script>
<script type="text/javascript">
    $(document).ready(function(){
            set_pageslink_onclick();        
    });
</script>
<?php
    $template->createPage('Solution view');
?>